//
//  MYSystemAlertView.m
//  JQLive
//
//  Created by Bruce on 2020/5/4.
//  Copyright © 2020 JQLive. All rights reserved.
//

#import "MYSystemAlertView.h"

@interface MYSystemAlertView ()
{
    UIView *my_mainView;
    UILabel *my_titleLabel;
    UILabel *my_contentLabel;
    
    UIButton *my_leftButton;
    UIButton *my_rightButton;
    
    UIView *line2View;
}
@property (nonatomic, copy) MYSystemAlertViewCompletion completion;
@property (nonatomic, copy) MYSystemAlertViewLeftClick leftClick;
@end

@implementation MYSystemAlertView

/// 对左右2个按钮不同响应
/// @param completion 右边按钮响应传递事件
/// @param leftClick 左边按钮响应传递事件
- (void)showWithCompletion:(MYSystemAlertViewCompletion)completion leftClick:(MYSystemAlertViewLeftClick)leftClick{
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    self.completion = completion;
    self.leftClick = leftClick;
}
- (void)showWithCompletion:(MYSystemAlertViewCompletion)completion
{
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    self.completion = completion;
}

- (void)showOnlyOneButtonWithCompletion:(MYSystemAlertViewCompletion)completion
{
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    self.completion = completion;
    
    my_leftButton.hidden = YES;
    line2View.hidden = YES;
    
    [my_rightButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@42);
        make.right.left.equalTo(self->my_mainView);
        make.top.equalTo(self->my_contentLabel.mas_bottom).offset(30);
    }];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    if(self = [super initWithFrame:frame])
    {
        [self layout];
    }
    
    return self;
}

- (void)layout
{
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    
    my_mainView = [[UIView alloc] init];
    my_mainView.backgroundColor = [UIColor whiteColor];
    my_mainView.clipsToBounds = YES;
    my_mainView.layer.cornerRadius = 8;
    [my_mainView addBouncesAnimation];
    [self addSubview:my_mainView];
    
    my_titleLabel = [[UILabel alloc] init];
    my_titleLabel.font = [UIFont boldSystemFontOfSize:16];
    my_titleLabel.textColor = MY_Text_Color_Theme1;
    my_titleLabel.numberOfLines = 0;
    my_titleLabel.text = @"确定要删除此订单？";
    my_titleLabel.textAlignment = NSTextAlignmentCenter;
    [my_mainView addSubview:my_titleLabel];
    
    my_contentLabel = [[UILabel alloc] init];
    my_contentLabel.font = [UIFont boldSystemFontOfSize:14];
    my_contentLabel.textColor = MY_Text_Color_Theme3;
    my_contentLabel.numberOfLines = 0;
    my_contentLabel.textAlignment = NSTextAlignmentCenter;
    [my_mainView addSubview:my_contentLabel];
    
    my_leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [my_leftButton setTitle:@"取消" forState:0];
    [my_leftButton setTitleColor:MY_Text_Color_Theme1 forState:0];
    my_leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [my_leftButton addTarget:self action:@selector(leftButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    my_leftButton.backgroundColor = [UIColor whiteColor];
    [my_mainView addSubview:my_leftButton];
    
    my_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [my_rightButton setTitle:@"确定" forState:0];
    [my_rightButton setTitleColor:MY_ColourMatchingSpecifications_Theme2 forState:0];
    my_rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [my_rightButton addTarget:self action:@selector(rightButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [my_mainView addSubview:my_rightButton];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = MY_ColourMatchingSpecifications_Line;
    [my_mainView addSubview:lineView];
    
    line2View = [[UIView alloc] init];
    line2View.backgroundColor = MY_ColourMatchingSpecifications_Line;
    [my_mainView addSubview:line2View];
    
    {
        [my_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(MY_Padding));
            make.right.equalTo(@(-MY_Padding));
            make.top.equalTo(@30);
        }];
        
        [my_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(MY_Padding));
            make.right.equalTo(@(-MY_Padding));
            make.top.equalTo(self->my_titleLabel.mas_bottom).offset(8);
        }];
        
        [my_leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@42);
            make.right.equalTo(self->my_mainView.mas_centerX);
            make.left.equalTo(@0);
            make.top.equalTo(self->my_contentLabel.mas_bottom).offset(30);
        }];
        
        [my_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.height.equalTo(my_leftButton);
            make.left.equalTo(my_leftButton.mas_right);
            make.right.equalTo(@0);
        }];
        
        [my_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@250);
            make.center.equalTo(self);
            make.top.equalTo(self->my_titleLabel.mas_top).offset(-30);
            make.bottom.equalTo(my_rightButton.mas_bottom);
        }];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self->my_mainView);
            make.height.equalTo(@(0.5));
            make.top.equalTo(self->my_leftButton);
        }];
        
        [line2View mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self->my_leftButton);
            make.width.equalTo(@(0.5));
            make.centerX.equalTo(self->my_mainView);
        }];
    }
}

#pragma mark-

- (void)setMy_rightButtonTextColor:(UIColor *)my_rightButtonTextColor
{
    [my_rightButton setTitleColor:my_rightButtonTextColor forState:0];
}

- (void)setMy_rightButtonTitle:(NSString *)my_rightButtonTitle
{
    [my_rightButton setTitle:my_rightButtonTitle forState:0];
}

//
- (void)setMy_leftButtonTextColor:(UIColor *)my_leftButtonTextColor
{
    [my_leftButton setTitleColor:my_leftButtonTextColor forState:0];
}

- (void)setMy_leftButtonTitle:(NSString *)my_leftButtonTitle
{
    [my_leftButton setTitle:my_leftButtonTitle forState:0];
}

- (void)setMy_content:(NSString *)my_content
{
    my_contentLabel.text = my_content;
}
- (void)setContentTextAlignment:(NSTextAlignment)contentTextAlignment
{
    _contentTextAlignment = contentTextAlignment;
    my_contentLabel.textAlignment = contentTextAlignment;
}

- (void)setMy_title:(NSString *)my_title
{
    my_titleLabel.text = my_title;
}

- (void)setMy_bgColor:(UIColor *)my_bgColor
{
    self.backgroundColor = my_bgColor;
}
// 设置content 字体颜色
- (void)setMy_contentColor:(UIColor *)my_contentColor{
    my_contentLabel.textColor = my_contentColor;
}
// 设置 leftButton 字体大小
- (void)setMy_leftButtonFont:(UIFont *)my_leftButtonFont{
    my_leftButton.titleLabel.font = my_leftButtonFont;
}
// 设置 rightButton 字体大小
- (void)setMy_rightButtonFont:(UIFont *)my_rightButtonFont{
    my_rightButton.titleLabel.font = my_rightButtonFont;
}
// 设置 content 字体大小
- (void)setMy_contentFont:(UIFont *)my_contentFont{
    my_contentLabel.font = my_contentFont;
}
- (void)leftButtonTouchUpInside:(UIButton *)sender
{
    if (self.leftClick) {
        self.leftClick(self);
    }
    [self dismiss];
}

- (void)rightButtonTouchUpInside:(UIButton *)sender
{
    if (self.completion)
    {
        self.completion(self);
    }
    
    [self dismiss];
}

- (void)dismiss
{
    //点击关闭
    [UIView animateWithDuration:0.20 animations:^{
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.20 animations:^{
            self.alpha = 0.0;
        } completion:^(BOOL finished) {
            self.hidden = YES;
            [self removeFromSuperview];
        }];
    }];
}


@end
